package com.zhixinhuixue.zsyte.xxx.ui.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gyf.immersionbar.ktx.immersionBar
import com.zhixinhuixue.library.common.ext.dp
import com.zhixinhuixue.zsyte.xxx.R
import com.zhixinhuixue.zsyte.xxx.app.api.NetUrl
import com.zhixinhuixue.zsyte.xxx.app.base.BaseNewFragment
import com.zhixinhuixue.zsyte.xxx.data.response.MainEntity
import com.zhixinhuixue.zsyte.xxx.databinding.FragmentOneBinding
import com.zhixinhuixue.zsyte.xxx.ui.activity.JWWLCXActivity
import com.zhixinhuixue.zsyte.xxx.ui.activity.SMTActivity
import com.zhixinhuixue.zsyte.xxx.ui.activity.WLCKActivity
import com.zhixinhuixue.zsyte.xxx.ui.activity.WLSJActivity
import com.zhixinhuixue.zsyte.xxx.ui.adapter.TestAdapter
import com.zhixinhuixue.zsyte.xxx.ui.viewmodel.TestViewModel
import me.hgj.mvvmhelper.ext.*
import me.hgj.mvvmhelper.net.LoadStatusEntity
import me.hgj.mvvmhelper.util.decoration.DividerOrientation

/**
 * 作者　: hegaojian
 * 时间　: 2020/11/18
 * 描述　:
 */
class OneFragment : BaseNewFragment<TestViewModel, FragmentOneBinding>(), OnItemClickListener {


    private val testAdapter: TestAdapter by lazy {
        TestAdapter()
    }

    override fun initView(savedInstanceState: Bundle?) {


        mViewBind.listSmartRefresh.refresh {
            //下拉刷新
            mViewModel.getList(true)

        }.setEnableLoadMore(false)
        //初始化recyclerview
        mViewBind.listRecyclerView.run {
            grid(2)
            divider {
                setColor(getColorExt(R.color.colorWhite))
                setDivider(10.dp)
                includeVisible = true
                orientation = DividerOrientation.GRID
            }
            adapter = testAdapter
        }

        onLoadRetry()
        mViewModel.getBanner()
        testAdapter.setOnItemClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        immersionBar {
            titleBar(mViewBind.userHeadImg)
        }
    }

    override fun onLoadRetry() {
        super.onLoadRetry()
        mViewModel.getList(true)
    }

    /**
     * 请求成功
     */
    override fun onRequestSuccess() {
        mViewModel.listData.observe(this, Observer {
            mViewBind.listSmartRefresh.finishRefresh()
            var list: MutableList<MainEntity> = ArrayList()
            it.forEach { item ->
                if (item.ParentId != null) {
                    list.add(item)
                }
            }
            testAdapter.setList(list)
        })

        mViewModel.bannerData.observe(this, Observer {
            var imgs = ""
            it.forEach { item ->
                imgs = item.PathStr
                mViewBind.tvName.text = item.Name
            }

            if (!TextUtils.isEmpty(imgs)) {
                Glide.with(this)
                    .load(imgs)
                    .centerCrop()
                    .into(mViewBind.userHeadImg)
            }


        })
    }

    /**
     * 请求失败
     * @param loadStatus LoadStatusEntity
     */
    override fun onRequestError(loadStatus: LoadStatusEntity) {
        when (loadStatus.requestCode) {
            NetUrl.HOME_LIST -> {
                mViewBind.listSmartRefresh.finishRefresh()
                showErrorUi(loadStatus.errorMessage)
            }
        }
    }

    override fun onBindViewClick() {
//        setOnclickNoRepeat(mViewBind.userHeadImg) {
//            when (it.id) {
//                R.id.userHeadImg -> {
//                    toStartActivity(LoginActivity::class.java)
//                }
//            }
//        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when (testAdapter.getItem(position).Code) {
            "M01001" -> {
                toStartActivity(WLSJActivity::class.java)
            }
            "M01002" -> {
                toStartActivity(WLCKActivity::class.java)
            }

            "M01003" -> {
                toStartActivity(JWWLCXActivity::class.java)
            }
            "M01004" -> {
                var bundle: Bundle = Bundle()
                bundle.putInt("type", 1);
                toStartActivity(SMTActivity::class.java, bundle)
            }
            "M01005" -> {
                var bundle: Bundle = Bundle()
                bundle.putInt("type", 2);
                toStartActivity(SMTActivity::class.java, bundle)
            }
        }
    }
}